/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public final class Data {

    public List<String> mFIDs = new ArrayList<String>();
    public List<String> mBiFIDs = new ArrayList<String>();
    public List<String> tagFame = new ArrayList<String>();
    public List<String> tagFameLarge = new ArrayList<String>();
    public Map<String, String> fameIDandTag = new HashMap<String, String>();
    public List<String> mediaIDs = new ArrayList<String>();
    public List<String> agencyIDs = new ArrayList<String>();
    public List<String> zoombieIDs = new ArrayList<String>();
    public List<User> userinfo = new ArrayList<User>();
    public List<String> washOutIDs = new ArrayList<String>();
    public List<String> nodes = new ArrayList<String>();
    public List<String> nodesHaveOut = new ArrayList<String>();
    public List<String> nodesHaveNoOut = new ArrayList<String>();
    public Map<String, String> username = new HashMap<String, String>();
    public Map<String, String> username2id = new HashMap<String, String>();
    public String Uid;
    public Map<String, Integer> biFCount = new HashMap<String, Integer>();
    public Map<String, Integer> comment = new HashMap<String, Integer>();
    public Map<String, Integer> repost = new HashMap<String, Integer>();
    public Map<String, Integer> latest = new HashMap<String, Integer>();
    public Map<String, List<String>> tag = new HashMap<String, List<String>>();
    public int celebrityCount = 0;
    public int normalCount = 0;
    public int agencyCount = 0;
    public int zoombieCount = 0;

    public Data(String path, String uid) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Uid = uid;
        readFameIDs();
        //readMediaIDs();
        readAgencyIDs();
        readZoombieIDs();
        //readFriendsInfo();
        readFriendIDs(path + "myFriendsIDs&Names" + ".txt");//初始化我的所有好友和不可访问好友
        readBiFriendIDs(path + "myBiFriendsIDs&Names" + ".txt");//初始化我的互粉，已经除去了不可访问好友
        cNetwork();//构建网络节点
        cNodesHaveOut(path + "outNet.txt");//有共同好友的节点、互粉的节点，除去了不可访问好友
        cNodesHaveNoOut();//没有共同好友的节点，同时也不是互粉，除去了不可访问好友
        Collections.sort(mFIDs);
        Collections.sort(mBiFIDs);
        Collections.sort(nodes);
        Collections.sort(nodesHaveOut);
        Collections.sort(nodesHaveNoOut);
    }

    public void readFameIDs() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\fame.txt")));
        String line = reader.readLine();
        while (line != null) {
            if (!line.isEmpty()) {
                String[] s = line.split("\t");
                if (!tagFame.contains(s[1])) {
                    tagFame.add(s[1]);
                }
                if (fameIDandTag.containsKey(s[0])) {
                    s[1] += (fameIDandTag.get(s[0]) + " ");
                }
                fameIDandTag.put(s[0], s[1]);
            }
            line = reader.readLine();
        }
        reader.close();
        for (String s : tagFame) {
            String[] bigCategory = s.split("/");
            if (tagFameLarge.contains(bigCategory[0])) {
                continue;
            } else {
                tagFameLarge.add(bigCategory[0]);
            }
        }
    }

    public void readMediaIDs() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\media.txt")));
        String line = reader.readLine();
        while (line != null) {
            if (!line.isEmpty()) {
                mediaIDs.add(line);
            }
            line = reader.readLine();
        }
        reader.close();
    }

    public void readAgencyIDs() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\agencies.txt")));
        String line = reader.readLine();
        while (line != null) {
            if (!line.isEmpty()) {
                String s[] = line.split("\t");
                agencyIDs.add(s[0]);
            }
            line = reader.readLine();
        }
        reader.close();
    }

    public void readZoombieIDs() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\Sina\\weibo4j-oauth2-beta\\src\\network\\data\\Zoombie.txt")));
        String line = reader.readLine();
        while (line != null) {
            if (!line.isEmpty()) {
                String[] s = line.split(" ");
                zoombieIDs.add(s[0]);
                String[] column = {"uid", "username"};
                String[] value = {s[0], s[1]};
                //SQL.SQL.SQL(SQL.SQL.insert(column, value, "wb_zoombie"));
            }
            line = reader.readLine();
        }
        reader.close();
    }

    public void readFriendsInfo() throws WeiboException {
        Users um = new Users();
        Friendships fm = new Friendships();
        String uid = Config.uid;
        User me = um.showUserById(uid);
        int fcount = me.getFriendsCount();
        int pages = fcount / 200 + 1;
        for (int i = 1; i <= pages; i++) {
            List<User> users = fm.getFriendsByID(uid, 200, i);
            for (User s : users) {
                userinfo.add(s);
            }
        }
    }

    public void readFriendIDs(String filename) throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String line = reader.readLine();
        username.put(Uid, Config.username);
        username.put("0", "0");
        username2id.put("0", "0");
        String a[];
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                if (a.length == 1) {
                    washOutIDs.add(a[0]);
                } else {
                    username.put(a[0], a[1]);
                    username2id.put(a[1], a[0]);
                    mFIDs.add(a[0]);
                    if (zoombieIDs.contains(a[0])) {
                        zoombieCount += 1;
                    } else if (fameIDandTag.containsKey(a[0])) {
                        celebrityCount += 1;
                    } else if (agencyIDs.contains(a[0])) {
                        agencyCount += 1;
                    } else {
                        normalCount += 1;
                    }
                }
                line = reader.readLine();
            } else {
                line = reader.readLine();
                continue;
            }
        }
        reader.close();
        mFIDs.remove("");
        mFIDs.add(Uid);
        mFIDs.add("0");
    }

    public void readBiFriendIDs(String filename) throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String line = reader.readLine();
        String a[];
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                if (a.length > 1) {
                    mBiFIDs.add(a[0]);
                }
                line = reader.readLine();
            } else {
                line = reader.readLine();
                continue;
            }
        }
        reader.close();
        mBiFIDs.remove("");
        mBiFIDs.add("0");
    }

    private void cNetwork() {
        for (String s : mFIDs) {
            nodes.add(s);
        }
    }

    private void cNodesHaveOut(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        List<String> NodesHaveOut = new ArrayList<String>();
        String line = reader.readLine();
        String start = "";
        String a[];
        while (line != null) {
            if (!line.isEmpty()) {
                a = line.split(" ");
                if (!start.equals(a[0])) {
                    NodesHaveOut.add(a[0]);
                }
                line = reader.readLine();
                start = a[0];
            } else {
                line = reader.readLine();
                continue;
            }
        }
        reader.close();
        for (String s : mBiFIDs) {
            if (!NodesHaveOut.contains(s)) {
                NodesHaveOut.add(s);
            }
        }
        NodesHaveOut.remove("");
        for (String s : NodesHaveOut) {
            if (nodes.contains(s)) {
                nodesHaveOut.add(s);
            }
        }
        nodesHaveOut.remove("");
    }

    private void cNodesHaveNoOut() {
        for (String s : nodes) {
            nodesHaveNoOut.add(s);
        }
        for (String s : nodesHaveOut) {
            if (nodes.contains(s)) {
                nodesHaveNoOut.remove(s);
            }
        }
    }
}
